import { Select, SelectProps } from "antd";
import { useImperativeHandle, useState } from "react";
import { ILawsRef } from "../../types";
import { useGetJdUnit } from "../../service/jd-unit-service";



interface IJdUnitSelectProps extends SelectProps<string> {
    handleChange: (value: string) => void;
    onRef?: React.MutableRefObject<ILawsRef | undefined>;
}
const JdUnitSelect = ({ handleChange, onRef }: IJdUnitSelectProps) => {
    /**
     *!获取街道HOOK
     */
    const { isLoading, isError, data: dataSource, error, refetch } = useGetJdUnit({
        limit: -1,
        page: 1
    });
    const [val, setVal] = useState<string>();
    const onChange = (value: string) => {
        setVal(value);
        handleChange(value);
    };

    /**向外暴露的重置方法 */
    useImperativeHandle(onRef, () => ({
        rest: () => {
            setVal(undefined);
        },
        fill: (jdUnitId: string) => {
            setVal(jdUnitId);
        },
        getData: () => {
            return ""
        }
    }))
    return <Select placeholder={'选择街道'} value={val} onChange={onChange} loading={isLoading}>
        {
            isError
                ? null
                : dataSource?.list.map((data) => {
                    return (
                        <Select.Option key={data.jdUnitId} value={data.jdUnitId}>
                            {data.jdUnitName}
                        </Select.Option>
                    );
                })
        }
    </Select >
}





export default JdUnitSelect;